# encoding: utf-8

import os
import codecs

def main(env):
	string = env["context"]["selection"].encode("utf8").replace("'",r"""'"'"'""") # is there a better way to deal with single quotes for AppleScript?
	source = "\n".join([
		'tell application "System Events"',
		'  set stdin to do shell script "cat"',
		'  set the clipboard to stdin',
		'  tell application "%s" to activate' % env["meta"]["richSuffix"],
		'  keystroke "v" using command down',
		'  keystroke tab using command down',
		'end tell'
	])
	codecs.open("log.scpt","w","utf8").write(source)
	os.system("echo '%s' | osascript log.scpt" % string)
	return [("moveCursor",0)]
